function ajax (method, url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest()
    // 响应体数据格式为jsonp格式
    xhr.responseType = 'json'
    // xhr初始化
    xhr.open(method, url, true)
    // readystate状态回调
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          resolve(JSON.parse(this.responseText))
        } else if (xhr.status === 404) {
          reject(new Error('404 not found'))
        }
      }
    }
    xhr.send(null)
  })
}

